<template>
    <div class="CommentPage">
        <div class="left">
            <img :src="e.aut_photo" alt="">
        </div>
        <div class="right">
            <div class="top">
                <p class="name">{{ e.aut_name }}</p>
                <span class="like">
                    <span class="count" ref="likeCount">{{ e.like_count }}</span>
                    <img @click="clickLike" :src="isLike ? likeIcon_cel : likeIcon" alt="">
                </span>
            </div>
            <div class="bottom">
                <p class="content">{{ e.content }}</p>
                <div class="tag">
                    <div>
                        <span class="reply" @click="showReply">
                            {{ e.reply_count }} 回复
                            <img src="@/assets/img/arrow-right.svg" alt=""></span>
                        <span class="time">{{ e.pubdate }}</span>
                    </div>
                    <img src="@/assets/img/btn_channel_close.svg" alt="" class="close">
                </div>
            </div>
        </div>
        <!-- 评论回复弹出页 -->
        <!--
        closeable
        close-icon-position="top-left"
        -->
        <!--
        这个评论回复弹出页有个属性用于显示一个关闭按钮，但是他是用伪元素实现的
        他的z-index=1，但是不知道为啥他显示的时候比z-index更高的元素层级还高
        这导致了一个问题，在打开评论的回复编辑页时，他还显示在最顶层
        我尝试过把他的z-index改成-1，的确成功解决了显示穿帮的问题
        但是又有了一个新问题，他层级太低了，根本点不到他，但是又有了一个神奇的现象，虽然带你不到他，但是仍然看得到他
        -->
        <van-popup v-model="show"
                   round
                   position="bottom"
                   duration="0.2"
                   :style="{ width: '100%' , height: '95%'}">
            <img class="closeBtn" @click="show = false" src="@/assets/img/arrow-left.svg" alt="">
            <div class="comment-reply">
                <div class="comment">
                    {{ e.reply_count }}条回复
                </div>
                <!-- 当前评论 -->
                <div class="current">
                    <div class="left">
                        <img :src="e.aut_photo" alt="">
                    </div>
                    <div class="right">
                        <div class="top">
                            <p class="name">{{ e.aut_name }}</p>
                            <span class="like">
                                <span class="count" ref="likeCount">{{ e.like_count }}</span>
                                <img @click="clickLike" :src="isLike ? likeIcon_cel : likeIcon" alt="">
                            </span>
                        </div>
                        <div class="bottom">
                            <p class="content">{{ e.content }}</p>
                            <div class="tag">
                                <div>
                                    <span class="time">{{ e.pubdate }}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div style="font-size: 18px; margin: 26px 16px 16px">全部回复</div>
                <!-- 遍历此评论的回复 -->
                <div class="reply" v-for="item in replyData" :key="item.com_id">
                    <div class="left">
                        <img :src="item.aut_photo" alt="">
                    </div>
                    <div class="right">
                        <div class="top">
                            <p class="name">{{ item.aut_name }}</p>
                            <span class="like">
                                <span class="count">{{ item.like_count }}</span>
                                <img @click="clickLike" :src="isLike ? likeIcon_cel : likeIcon" alt="">
                            </span>
                        </div>
                        <div class="bottom">
                            <p class="content">{{ item.content }}</p>
                            <div class="tag">
                                <span class="time">{{ item.pubdate }}</span>
                                <img src="@/assets/img/btn_channel_close.svg" alt="" class="close">
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 空状态 -->
                <van-empty v-if="e.reply_count === 0" description="暂时还没有人回复"/>

                <!-- 底部操作栏 -->
                <div class="footer">
                    <!-- 搜索框 -->
                    <van-search
                        @click.native="showEdit = true"
                        :left-icon="editIcon"
                        shape="round"
                        placeholder="写回复..."
                        @input="1"
                    />
                    <!-- 评论编辑弹出页 -->
                    <comment-edit :showEdit="showEdit"
                                  :commentId="this.e.com_id"
                                  :artId="artId"
                                  :target="'comment'"
                                  @closeShowEdit="showEdit = false"
                                  @initDetail="showReply"
                                  @reload="$emit('reload')">
                    </comment-edit>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<script>
import { getLikeComment, delLikeComment, getComment } from '@/api/comment'

export default {
    name: 'CommentPage',
    props: {
        // 此评论的数据
        e: Object,
        artId: true
    },
    data () {
        return {
            // 评论回复的数据
            replyData: [],
            // 此评论是否点赞
            isLike: this.e.is_liking,
            show: false,
            showEdit: false,
            editIcon: require('@/assets/img/圆角-编辑.png'),
            likeIcon: require('@/assets/img/btn_like.svg'),
            likeIcon_cel: require('@/assets/img/btn_like_sel.svg')
        }
    },
    methods: {
        async clickLike () {
            if (this.isLike) {
                await delLikeComment(this.e.com_id)
                this.isLike = !this.isLike
                let count = +this.$refs.likeCount.innerHTML
                // 此评论点赞数-1
                this.$refs.likeCount.innerHTML = count - 1
            } else {
                await getLikeComment(this.e.com_id)
                this.isLike = !this.isLike
                let count = +this.$refs.likeCount.innerHTML
                // 此评论点赞数+1
                this.$refs.likeCount.innerHTML = count + 1
            }
        },
        // 加载此评论的回复
        async showReply () {
            this.show = true
            const res = await getComment({
                type: 'c',
                source: this.e.com_id,
                limit: 20
            })
            this.replyData = res.data.data.results
        }
    }
}
</script>

<style scoped lang="less">
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.current {
    border-bottom: 1px solid #F0F0F0;
    .time {
        margin: 0 !important;
    }
}

.CommentPage, .current {
    display: flex;
    padding: 16px;

    .left {
        :first-child {
            width: 34px;
            height: 34px;
            border-radius: 17px;
            object-fit: cover;
            margin-right: 12px;
        }
    }

    .right {
        flex: 1;

        .top {
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .name {
                font-size: 15px;
                color: #333333;
                line-height: 15px;
                margin: 0;
            }

            .like {
                .count {
                    width: 14px;

                    + img {
                        width: 14px;
                        margin-left: 8px;
                    }
                }
            }
        }

        .bottom {
            .content {
                margin: 3px 0 15px;
            }

            .tag {
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 12px;
                color: #333333;
                line-height: 12px;

                > div {
                    display: flex;
                    align-items: center;

                    + img {
                        width: 8px;
                    }
                }

                .reply {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 61px;
                    height: 25px;
                    background-color: #F7F8FA;
                    border-radius: 12.5px;

                    > img {
                        width: 8px;
                        height: 8px;
                        margin-left: 6px;
                    }
                }

                .time {
                    font-size: 12px;
                    color: #A5A6AB;
                    line-height: 12px;
                    margin-left: 12px;
                }
            }
        }
    }

    .closeBtn {
        position: absolute;
        top: 14px;
        left: 10px;
        color: #333333;
        height: 15px;
    }

    .comment-reply {
        .comment {
            height: 42px;
            text-align: center;
            line-height: 42px;
            font-size: 17px;
            font-weight: 500;
            border-bottom: 1px solid #F0F0F0;
        }

        .reply {
            display: flex;
            padding: 16px;

            .right {
                .bottom {
                    .tag {
                        .time {
                            margin: 0;
                        }

                        img {
                            width: 8px;
                            height: 8px;
                            margin-left: 6px;
                        }
                    }
                }
            }
        }

        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            height: 44px;
            background-color: white;
            display: flex;
            align-items: center;
            border: 1px solid #F0F0F0;
            padding: 0 15px;

            ::v-deep .van-search {
                width: 100%;
            }

            ::v-deep .van-icon__image {
                margin: 3px 5px 0 10px;
            }
        }
    }

    ::v-deep .van-popup__close-icon.van-popup__close-icon--top-left {
        top: 10px;
        left: 10px;
        z-index: 1;
        color: #333333;
    }
}

</style>
